package com.qkst.website.web.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.backstage.bean.ProductBean;
import com.qkst.website.common.core.BaseController;
import com.qkst.website.common.core.Pager;
import com.qkst.website.common.utils.SetEnums;
import com.qkst.website.web.bean.BargainLogBean;
import com.qkst.website.web.bean.OrderBean;
import com.qkst.website.web.bean.UserBean;
import com.qkst.website.web.service.BargainService;
import com.qkst.website.web.service.OrderService;
import com.qkst.website.web.service.ProductService;

/**
 * @author lihao
 * @description 订单控制器
 * @date 2017-05-03
 *
 */
@Controller
@RequestMapping("/web/order")
public class WebOrderController extends BaseController{


	@Autowired
	private OrderService orderService;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private BargainService bargainService;//砍价 service
	
	/**
	 * 订单列表页面数据查找
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list.html", method = { RequestMethod.GET })
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,OrderBean order) throws IOException{
		ModelAndView mv= this.webModelAndView("order/list");
		order.setPage(1);
		order.setRows(10);
		Pager pager = new Pager();
		UserBean loginUser=(UserBean) request.getSession().getAttribute(SetEnums._LOGIN_USER.toString());
		order.setUser(loginUser);
		order.setUserId(loginUser.getUserId());//设置当前登陆用户
		//分页查询订单列表
		List<OrderBean> orderList=orderService.queryOrderList(order);
		//order总记录数
		int records=orderService.queryCount(order);
		pager.setRecords(records);
		mv.addObject("orderList", orderList);//返回订单列表
		mv.addObject("pager", pager);//返回总记录数据
		return mv;
	}
	
	/**
	 * 订单列表页面数据查找
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> do_list(HttpServletRequest request,HttpServletResponse response,OrderBean order) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		order.setPage(1);
		order.setRows(10);
		Pager pager = new Pager();
		UserBean loginUser=(UserBean) request.getSession().getAttribute(SetEnums._LOGIN_USER.toString());
		order.setUser(loginUser);
		order.setUserId(loginUser.getUserId());//设置当前登陆用户
		//分页查询订单列表
		List<OrderBean> orderList=orderService.queryOrderList(order);
		//order总记录数
		int records=orderService.queryCount(order);
		pager.setRecords(records);
		result.put("orderList", orderList);
		result.put("total",records);
		result.put("pager", pager);
		return result;
	}
	
	/**创建订单
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/createOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> createOrder(HttpServletRequest request,HttpServletResponse response,@RequestBody OrderBean order) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		//保存订单
		orderService.save(order,result);
		return result;
	}
	/**取消订单
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/cancelOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> cancelOrder(HttpServletRequest request,HttpServletResponse response,OrderBean order) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		//取消订单
		orderService.cancelOrder(order,result);
		return result;
	}
	
	/**立即购买
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/buyNow.do", method = { RequestMethod.GET })
	public ModelAndView buyNow(HttpServletRequest request,HttpServletResponse response,String partnum) throws IOException{
		ModelAndView mv= this.webModelAndView("order/payment");
		//查询产品根据编号
		ProductBean product=productService.queryProductByPartnum(partnum);
		UserBean loginUser=this.getLoginUser(request, response);
		//查询砍价金额
		BargainLogBean bargain = new BargainLogBean();
		bargain.setBuyer(loginUser);
		bargain.setProduct(product);
		bargain = bargainService.queryBargainLogByConditions(bargain);
		mv.addObject("product", product);//返回产品
		mv.addObject("bargain", bargain);//返回砍价记录
		return mv;
	}
	
	

}
